<?php
namespace backend\modules\admin\models;

use yii\db\ActiveRecord;
class Menu extends ActiveRecord
{
    /**
     * Role类关联数据库表名称 menu
     */
    public static function tableName()
    {
        return '{{%menu}}';
    }
     /**
     * 规则
     */
    public function rules()
    {
        return [
            [['name','cname','m','c','a'],'required','message'=>'必填'],
            ['name','unique','message'=>'英文名重复'],
            [['icon','data'],'default','value'=>''],
            [['listorder','display','parentid','status'],'integer','message'=>'必须为整数'],
        ];
    }

    /**
     * 属性标签
     */
    public function attributeLabels()
    {
        return [
            'data'=>'附加参数',
            'parentid'=>'上级菜单',
            'name' => '菜单英文名称',
            'cname' => '菜单中文名称',
            'm'=>'模块名',
            'c'=>'文件名',
            'a'=>'方法名',
            'icon'=>'图标',
            'listorder'=>'排序',
            'display' => '是否显示菜单',
        ];
    }
    /**
     * 是否启用
     */
    public function whetherDisabled()
    {
        if($this->display=='1')
        {
            return '是';
        }
        if($this->display=='0')
        {
            return '否';
        }
    }
    
    public function afterSave($insert,$changedAttributes)
    {
        if($insert || $changedAttributes)
        {
            if($this->parentid != 0)
            {
                $id=$this->id;//获取当前插入菜单的id
                $parent = self::findOne(['id'=>$this->parentid]);//获取当前菜单的父菜单
                $ids = $parent->arrchildid;//获取父菜单的arrchildid属性值
                if(empty($ids))
                {
                    $parent->arrchildid = $id;//赋值 
                }
                else
                {
                    $idsarray =  explode(',',$ids);//把获取的值拆分成数组
                    if(!in_array($id,$idsarray))//判断如果获取的属性值不在数组中
                    {
                            $idsarray[]=$id;//赋值
                            $ids=implode(',',$idsarray);//拆分成字符串
                            $parent->arrchildid=$ids;//赋值

                    }
                }
                $parent->save();
            }
        }
        if($this->status == -1)//判断status是否等于1
        {
            $childs = self::find()->where('parentid='.$this->id)->all();//查询出所有parentid等于当前删除菜单id
            if($childs)
            {
                foreach($childs as $child)//循环遍历所有数据
                {
                    self::updateAll(['status' => -1], ['id'=>$child->id]);//将该栏目下所有的子栏目删除
                    $chil = self::find()->where('parentid='.$child->id)->all();//查询该栏目下的子栏目还存不存在子栏目
                    if($chil)
                    {
                        foreach($chil as $chi)
                        {
                            self::updateAll(['status' => -1], ['id'=>$chi->id]);
                        }
                    }
                }
            }
        }
        return true;
    }
    /**
     * 获取菜单表信息
     * @param int $menuid 菜单ID
     * @param int $menu_info 菜单数据
     */
    public function get_menuinfo($menuid,$menu_info)
    {
        $menuid = intval($menuid);
        unset($menu_info[$menuid]['id']);
        return $menu_info[$menuid];
    }

    /**
     * 检查指定菜单是否有权限
     * @param array $data menu表中数组
     * @param int $roleid 需要检查的角色ID
     */
    public static function is_checked($data,$roleid,$siteid,$priv_data)
    {
        $priv_arr = array('m','c','a','data');
        if($data['m'] == '') return false;
        foreach($priv_data as $keys => $v)
        {
            unset($priv_data[$keys]['id']);
        }
		foreach($data as $key=>$value){
		    if(!in_array($key,$priv_arr)) unset($data[$key]);
		}
		$data['roleid'] = $roleid;
		$data['siteid'] = $siteid;
        $info = in_array($data, $priv_data);
		if($info){
			return true;
		} else {
			return false;
		}
    }

    /**
	 * 是否为设置状态
	 */
	public function is_setting($siteid,$roleid) {
		$siteid = intval($siteid);
		$roleid = intval($roleid);
		
        $resule = self::find()->where(['siteid'=>$siteid,'roleid'=>$roleid],'m is not null')->one();
		return $result ? true : false;
    }
    
	/**
	 * 获取菜单深度
	 * @param $id
	 * @param $array
	 * @param $i
	 */
	public static function get_level($id,$array=array(),$i=0) {
		foreach($array as $n=>$value){
			if($value['id'] == $id)
			{
				if($value['parentid']== '0') return $i;
				$i++;
				return self::get_level($value['parentid'],$array,$i);
			}
		}
	}
}
?>